package com.davidhigenbottam.HeatMap.server;

import java.io.IOException;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import com.davidhigenbottam.HeatMap.data.ProvinceData;
import com.davidhigenbottam.HeatMap.shared.Institution;
import com.davidhigenbottam.HeatMap.shared.Province;
import com.davidhigenbottam.HeatMap.shared.Query;
import com.davidhigenbottam.HeatMap.shared.ServerResults;

@SuppressWarnings("serial")
public class ExcelServlet extends HttpServlet {
	
	
	
//	private static final Logger log = Logger.getLogger(KMLServlet.class
//			.getName());
	
	

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		//resp.setContentType("text/csv");
		resp.setContentType("application/vnd.ms-excel");
		
		
		resp.setHeader("Content-disposition",
                "attachment; filename=indonesia_province_data.xls" );
		//TODO make filename specific to query

		
		Query q = new Query(req.getParameter("ds"), req.getParameter("y"),
				req.getParameter("l"), req.getParameter("it"),
				req.getParameter("io"));
		
		ProvinceData provinceData= null;
			
		DataCache cache= DataCache.getInstance();
		ServerResults res= cache.getData(q);
		provinceData= res.getProvinceData();
		
		Province[] provinces= provinceData.getAll();
		List<Institution> institutions= res.getInstitutionData().getList();
		
		 WritableWorkbook w = Workbook.createWorkbook(resp.getOutputStream());
	     WritableSheet s = w.createSheet("Indonesian Province Data", 0);
	     
	     try {
	    	s.addCell(new Label(0, 0, q.getLongDescription())); 
	    	
			s.addCell(new Label(0, 1, "Province"));
			s.addCell(new Label(1, 1, "Value"));
			s.addCell(new Label(2, 1, "Institution"));
			s.addCell(new Label(3, 1, "Value")); 
			
			for (int i=0;i<provinces.length;i++) {
				Province p = provinces[i];
			//	resp.getWriter().print(p.getName());
			//	resp.getWriter().print(",");
			//	resp.getWriter().println(p.getValueDescription());

				s.addCell(new Label(0, i+2, p.getName()));
				s.addCell(new jxl.write.Number(1, i+2, p.getVal()));
			}
			
			int idx=0;
			for(Institution i : institutions){
				s.addCell(new Label(2, idx+2, i.getName()));
				s.addCell(new jxl.write.Number(3, idx+2, i.getVal()));
				idx++;
			}
			
			 w.write();
			 w.close();
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}

		
		
	}

	/**
	 * generate a random hex color
	 * 
	 * @return
	 */
	public static String getRandomColor() {

		String hex1 = getRandomHex();
		String hex2 = getRandomHex();
		String hex3 = getRandomHex();
		String hex4 = getRandomHex();
		String hex5 = getRandomHex();
		String hex6 = getRandomHex();

		String color = "7f" + hex1 + hex2 + hex3 + hex4 + hex5 + hex6;

		return color;
	}

	/**
	 * get random hex
	 * 
	 * @return int
	 */
	private static String getRandomHex() {
		String[] hex = new String[] { "0", "1", "2", "3", "4", "5", "6", "7",
				"8", "9", "A", "B", "C", "D", "E", "F" };
		Random random = new Random();
		int randomNum = random.nextInt(hex.length);
		String sHex = hex[randomNum];
		return sHex;
	}



}
